<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>地址管理</title>
	<meta name="author" content="XUE">
	<meta name="description" content="普济一城">
	<meta name="viewport" content="initial-scale=1,maximum-scale=1">
	<meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="apple-mobile-web-app-status-bar-style" content="transparent">
	<meta name="format-detection" content="telephone=no,email=no">
	<link rel="stylesheet" type="text/css" href="../../css/base.css">
	<link rel="stylesheet" type="text/css" href="../../css/layer.css"/>
	<link rel="stylesheet" type="text/css" href="../../css/product-order/management.css?v=0.0.2">
</head>
<body>
	<!-- head -->
	<div class="head">
		<p>地址管理</p>
		<div class="head-img">
			<img src="../../img/d_share.png">
		</div>
	</div>
	<!-- main -->
	<div class="main">
	</div>
	<!-- foot -->
	<div class="foot">
		<button class="footBtn"><span class="jia">+</span>添加新收货人</button>
	</div>
</body>
<script type="text/javascript" src="../../js/zepto.min.js"></script>
<script src="../../js/layer.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" src="../../js/common.js"></script>
<script type="text/javascript">

	Auth();

	// 返回填写订单页面
	$('.head-img').on('click',function(){
		// window.location.href = rootdomain +'html/shopCart/txdd.html';
		window.history.go(-1);
	})
	// // 添加收货人 跳转到新增收货人
	$('.footBtn').on('click',function(){
		localStorage.setItem('bianji',2)
		window.location.href = 'newConsignee.html';
	})


	// 展示收货人列表
	function show(){
		var dataObj = {
			phone:user_info.phone,
			uuid:user_info.uuid
//			id:user_info.id
		}
		$.ajax({
			type:"post",
			url:apicom + "mobile/after/memberaddress/addresslist",
//		 	url:"http://192.168.30.231:10001/mobile/after/memberaddress/addresslist",
		 	data:dataObj,
		 	async: false,
		 	dataType: "json",
		 	success:function(res){
		 		console.log(res);
		 		if(typeof(res) == 'string'){
					res = JSON.parse(res);
				}

		 		if(res.code == 0){
		 			tips(res.msg)
		 		} else if(res.code == 1){
		 			$('.main').html("");

		 			for(var i = 0,len=res.content.length; i < len; i++){
			 			if(res.content.length == 1){
			 				res.content[i].is_default = 1;
			 			}
		 				var $elem = '';
		 				if(res.content[i].is_default == 0){
		 					$elem += '<div class="main-a sss" data-defaultAddress="'+ res.content[i].province_name + res.content[i].city_name + res.content[i].county_name + res.content[i].detail_address + '" data-addressId="'+ res.content[i].id +'" data-mobile="'+res.content[i].mobile+'" data-userName="'+res.content[i].user_name+'"><div class="zx-wai"><div class="contacts"><div class="contacts-a"><p class="username">'+res.content[i].user_name+'</p></div><p class="userphone">'+res.content[i].mobile+'</p></div><div class="address"><p><span class="provincename">'+res.content[i].province_name+'</span>,<span>'+res.content[i].city_name+'</span>,<span class="countyname">'+res.content[i].county_name+'</span>,<span class="cityname">'+res.content[i].detail_address+'</span></p></div></div><div class="main-b"><div class="mainbLeft"><div class="mainbLeft-pic" data-is_default='+res.content[i].is_default+' data-id='+res.content[i].id+'><img src="../../img/d_normal.png"></div><p class="setup">设为默认地址</p></div><div class="mainbRight">';
		 					$elem += "<div class='right-a' data-xx='"+res.content[i].province_name+'|'+res.content[i].city_name+'|'+res.content[i].county_name +'|'+res.content[i].detail_address+'|'+res.content[i].user_name+'|'+res.content[i].mobile+'|'+ res.content[i].id+'|'+res.content[i].is_default+"' data-id="+res.content[i].id+"><div class='right-pic'><img src='../../img/d_cart1.png'></div><p class='edit'>编辑</p></div><p class='hg'>|</p><div class='right-b' data-id="+res.content[i].id+"><div class='right-pic1'><img src='../../img/d_delete.png'></div><p class='delete'>删除</p></div></div></div>"
		 				} else if(res.content[i].is_default == 1){
		 					$elem += '<div class="main-a" data-defaultAddress="'+ res.content[i].province_name + res.content[i].city_name + res.content[i].county_name + res.content[i].detail_address + '" data-addressId="'+ res.content[i].id +'" data-mobile="'+res.content[i].mobile+'" data-userName="'+res.content[i].user_name+'"><div class="zx-wai"><div class="contacts"><div class="contacts-a"><p class="username">'+res.content[i].user_name+'</p></div><p class="userphone">'+res.content[i].mobile+'</p></div><div class="address"><p><span class="provincename">'+res.content[i].province_name+'</span>,<span>'+res.content[i].city_name+'</span>,<span class="countyname">'+res.content[i].county_name+'</span>,<span class="cityname">'+res.content[i].detail_address+'</span></p></div></div><div class="main-b"><div class="mainbLeft"><div class="mainbLeft-pic" data-is_default='+res.content[i].is_default+' data-id='+res.content[i].id+'><img src="../../img/d_select.png"></div><p class="setup">设为默认地址</p></div><div class="mainbRight">';
		 					$elem += "<div class='right-a' data-xx='"+ res.content[i].province_name+'|'+res.content[i].city_name+'|'+res.content[i].county_name+'|'+res.content[i].detail_address+'|'+res.content[i].user_name+'|'+res.content[i].mobile+'|'+res.content[i].id+'|'+res.content[i].is_default+"' data-id="+res.content[i].id+"><div class='right-pic'><img src='../../img/d_cart1.png'></div><p class='edit'>编辑</p></div><p class='hg'>|</p><div class='right-b' data-id="+res.content[i].id+"><div class='right-pic1'><img src='../../img/d_delete.png'></div><p class='delete'>删除</p></div></div></div>";

		 				}
		 				$('.main').append($elem);
		 			}
		 			// if($elem){
		 			// 	$elem.insertBefore($('.sss').eq(0))
		 			// }
		 		}
		 	},
		 	error:function(err){
		 		console.log(err)
		 	}
		})
	}
	show()


	//删除
	$('.right-b').on('click',function(event){
		 event.stopPropagation();
		var rwid = $(this).data('id')
		console.log(rwid)
		var dataObj = {
			id:rwid,
			uuid:user_info.uuid,
			phone:user_info.phone
		}
		$.ajax({
			type:"post",
		 	url:apicom + "mobile/after/memberaddress/deladdressbyid",
//		 	url:"http://192.168.30.231:10001/mobile/after/memberaddress/deladdressbyid",
		 	data:dataObj,
		 	async: false,
		 	dataType: "json",
		 	success:function(res){
		 		if(typeof(res) == 'string'){
					res = JSON.parse(res);
				}
		 		localStorage.setItem('ald',rwid)
		 		if(res.code == 1){
		 			console.log(res);
					$(this).parents('.main-a').remove();
					window.location.reload();
		 		} else{
		 			tips(res.msg)
		 		}
		 	},
		 	error:function(err){
		 		console.log(err)
		 	}
		})
	})



	// 编辑
	$('.main-a').on('click','.right-a',function(event){
		 event.stopPropagation();
		var rwid = $(this).data('id');
		var dz = $('.right-a').index(this)
		console.log(dz);

		localStorage.setItem("xxdz",$(this).attr('data-xx'))
		localStorage.setItem("bianji",1)
		console.log($(this).attr('data-xx'))


		var dataObj = {
			aid: rwid,
			uuid:user_info.uuid,
			phone:user_info.phone
//			id:user_info.id
		}
		$.ajax({
			type:"post",
		 	url: apicom + "mobile/after/memberaddress/toUpdateAddress",
//		 	url:"http://192.168.30.231:10001/mobile/after/memberaddress/toUpdateAddress",
		 	data:dataObj,
		 	async: false,
		 	dataType: "json",
		 	success:function(res){
		 		console.log(res)
		 		if(typeof(res) == 'string'){
					res = JSON.parse(res);
				}
		 		localStorage.setItem('cityAreaId',res.content.city_area_id);
		 		localStorage.setItem('districtAreaId',res.content.district_area_id);
		 		localStorage.setItem('proAreaId',res.content.pro_area_id);
		 		localStorage.setItem('ybdz',res.content.code);
				window.location.href = rootdomain +'html/product-order/newConsignee.html'

		 	},
		 	error:function(err){
		 		console.log(err)
		 	}
		})
	})

	//默认地址


	$('.mainbLeft-pic').on('click',function(event){
		 event.stopPropagation();
		$(this).toggleClass('active');
		var rwid = $(this).data('id')
		var dataObj = {
			aid:rwid,
			uuid:user_info.uuid,
			phone:user_info.phone
		}
		$.ajax({
			type:"post",
		 	url:apicom + "mobile/after/memberaddress/set-default-address",
//			url:"http://192.168.30.231:10001/mobile/after/memberaddress/set-default-address",
		 	data:dataObj,
		 	async: false,
		 	dataType: "json",
		 	success:function(res){
		 		if(typeof(res) == 'string'){
					res = JSON.parse(res);
				}
		 		console.log(res)
		 		show()
				//window.location.reload();
		 	},
		 	error:function(err){
		 		console.log(err)
		 	}
		})
	})

	var shopCartIds = sessionStorage.getItem('shopCartIds')
	shopCartIds = JSON.parse(shopCartIds)
	console.log(shopCartIds)
	$(document).on('click','.main-a',function(){
		console.log(shopCartIds)
		shopCartIds.defaultAddress = $(this).attr('data-defaultaddress');
		shopCartIds.mobile = $(this).attr('data-mobile');
		shopCartIds.userName = $(this).attr('data-userName');
		shopCartIds.addressId = $(this).attr('data-addressId');
		shopCartIds.addressFlag = 1;
		sessionStorage.setItem('shopCartIds',JSON.stringify(shopCartIds));
		window.location.href = rootdomain +'html/product-order/product-tjdd.html';
	})
</script>
</html>